package com.ev.apis.controller.custom;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.ev.apis.model.DsResultResponse;
import com.ev.custom.vo.UpdateLogPageParam;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.system.service.OpLogService;
import com.ev.system.vo.OpLogPageParam;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * 文件名称： com.ev.system.OpLogApiController.java</br>
 * 初始作者： ev-monitor</br>
 * 创建日期： 2020-06-04 16:52:15</br>
 * 功能说明： 操作日志表 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2021 .All rights reserved.<br/>
 */

@Api(value = "/", tags = "操作日志表API")
@RestController
public class OpLogApiController {

    /**
     * 操作日志表 服务类
     */
    @Autowired
    private OpLogService opLogService;

    /**
     * 方法描述: [根据条件分页查询操作日志表.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-06-04 16:52:15<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param pageParam 分页参数
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/opLog/pageList", method = RequestMethod.POST, apiTitle = "根据条件分页查询操作日志表")
    @ApiOperation(value = "根据条件分页查询操作日志表", produces = MediaType.APPLICATION_JSON_VALUE)
    public R pageList(@Valid @RequestBody @ApiParam("操作日志表分页列表参数对象") OpLogPageParam pageParam) {
        return R.ok(this.opLogService.pageList(pageParam));
    }
//
//    /**
//     * 方法描述: [根据id获取操作日志表.]</br>
//     * 初始作者: ev-monitor<br/>
//     * 创建日期: 2020-06-04 16:52:15<br/>
//     * 开始版本: 1.0.0<br/>
//     * =================================================<br/>
//     * 修改记录：<br/>
//     * 修改作者 日期 修改内容<br/>
//     * ================================================<br/>
//     *
//     * @param id 主键
//     * @return R 响应对象
//     */
//    @EvApiByToken(value = "/apis/opLog/detail/{id}", method = RequestMethod.GET, apiTitle = "根据id获取操作日志表")
//    @ApiOperation("根据id获取操作日志表")
//    public R detail(@PathVariable("id") @ApiParam("id") Long id) {
//        return R.ok(this.opLogService.getById(id));
//    }

    /**
     * 方法描述: [批量删除]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-06-04 16:52:15<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/opLog/batchRemove", method = RequestMethod.POST, apiTitle = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除", produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public R batchRemove(@Valid @RequestBody @ApiParam("操作日志表分页列表参数对象") OpLogPageParam pageParam) {
        opLogService.remove(pageParam);
        return R.ok();
    }

    @ResponseBody
    @EvApiByToken(value = "/apis/exportExcel/opLog", method = RequestMethod.GET, apiTitle = "日志(导出)")
    @ApiOperation("日志(导出)")
    public void badPurchase(@ApiParam(value = "开始时间") @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
                            @ApiParam(value = "结束时间") @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
                            @ApiParam(value = "用户名") @RequestParam(value = "userName", defaultValue = "", required = false) String userName,
            HttpServletRequest request, HttpServletResponse response,ModelMap modelMap
    ) {
        OpLogPageParam opLogPageParam = new OpLogPageParam();
        opLogPageParam.setEndTime(endTime);
        opLogPageParam.setStartTime(startTime);
        opLogPageParam.setUserName(userName);
        opLogPageParam.setPagesize(Integer.MAX_VALUE);
        Map<String, Object> resultMap = this.opLogService.pageList(opLogPageParam);
        if(resultMap.containsKey("data")){
            DsResultResponse data = (DsResultResponse)resultMap.get("data");
            ClassPathResource classPathResource = new ClassPathResource("poi/log.xls");
            Map<String,Object> map = Maps.newHashMap();
            map.put("list", data.getDatas());
            TemplateExportParams result = new TemplateExportParams(classPathResource.getPath());
            modelMap.put(TemplateExcelConstants.FILE_NAME, "日志");
            modelMap.put(TemplateExcelConstants.PARAMS, result);
            modelMap.put(TemplateExcelConstants.MAP_DATA, map);
            PoiBaseView.render(modelMap, request, response,
                    TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
        }
    }


    @EvApiByToken(value = "/apis/log/getUpdateLog", method = RequestMethod.POST, apiTitle = "获取更新日志")
    @ApiOperation(value = "获取更新日志", produces = MediaType.APPLICATION_JSON_VALUE)
    public R getUpdateLog(@Valid @RequestBody @ApiParam("更新日志参数") UpdateLogPageParam pageParam) {
        return R.ok(this.opLogService.getUpdateLog(pageParam));
    }

    @EvApiByToken(value = "/apis/log/getUpdateLog/{id}", method = RequestMethod.GET, apiTitle = "根据id获取更新日志")
    @ApiOperation("根据id获取更新日志")
    public R detail(@PathVariable("id") @ApiParam("id") Long id) {
        return R.ok(this.opLogService.getUpdateLogById(id));
    }

//    /**
//     * 方法描述: [保存操作日志表.]</br>
//     * 初始作者: ev-monitor<br/>
//     * 创建日期: 2020-06-04 16:52:15<br/>
//     * 开始版本: 1.0.0<br/>
//     * =================================================<br/>
//     * 修改记录：<br/>
//     * 修改作者 日期 修改内容<br/>
//     * ================================================<br/>
//     *
//     * @param saveParam 操作日志表参数
//     * @return R 响应对象
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @EvApiByToken(value = "/apis/opLog/save", method = RequestMethod.POST, apiTitle = "保存操作日志表")
//    @ApiOperation("保存操作日志表")
//    public R save(@Valid @RequestBody @ApiParam("保存操作日志表对象") OpLogDO saveParam) {
//        return this.opLogService.saveAndVerify(saveParam);
//    }

}
